package com.liu.eduService.Excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.CellData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liu.eduService.entity.EduSubject;
import com.liu.eduService.service.EduSubjectService;
import com.liu.serviceBase.exception.MyException;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.aop.ThrowsAdvice;

import java.util.Map;

/**
 * **************
 * 项目名称: testgulili <br/>
 * 文件名称:  <br/>
 * 文件描述: 这里添加您的类文件描述，说明当前文件要包含的功能。 <br/>
 * 文件创建：刘世锦 <br/>
 * 创建时间: 2022/1/6 <br/>
 *
 * @version v1.0 <br/>
 * @update [序号][日期YYYY-MM-DD][更改人姓名][变更描述]<br/>
 * *************
 */
public class ExcelListener extends AnalysisEventListener<ExcelData> {

    // 不将该类注册进 pring容器 的原因：在EduSubjectServiceImpl要我们自己new出来，
    // 而且 还要操作数据库，所以写上构造函数
    private EduSubjectService service;
//    使用service来将Excel的文件数据写到数据库中

//    public EduSubjectService getService() {
//        return service;
//    }
//
//    public void setService(EduSubjectService service) {
//        this.service = service;
//    }

    public ExcelListener(EduSubjectService service) {

        this.service = service;
    }
    public ExcelListener() { }


    @Override
//    将文件的数据读取 且 加入数据库
    public void invoke(ExcelData data, AnalysisContext context) {
        if (data ==null){
            throw new MyException(20001,"文件data为空");
        }
//        添加一级标题
        String firstName = data.getFirstName();
        System.out.println(data);
        System.out.println("++++++++++++++++"+firstName);
        EduSubject subject = this.IsExsistFirstNameInSQL(service, firstName);
        if(subject==null){
            subject = new EduSubject();
            subject.setTitle(firstName);
            subject.setParentId("0");
            service.save(subject);
        }

//        添加二级标题
        String secondName = data.getSecondName();
        //存在一级标题时，才可以添加二级标题 pid
        String pid = subject.getId();
        EduSubject subject2 = this.IsExsistSecondNameInSQL(service, secondName,pid);
        if(subject2==null){
            subject2 = new EduSubject();
            subject2.setTitle(secondName);
            subject2.setParentId(pid);
            service.save(subject2);
        }
    }
    //判断一级分类是否存在
    public EduSubject IsExsistFirstNameInSQL(EduSubjectService service, String title){
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("title",title);
        wrapper.eq("parent_id","0");
        EduSubject eduSubject =   service.getOne(wrapper);
        return  eduSubject;
    }

    //判断二级分类是否存在
    public EduSubject IsExsistSecondNameInSQL(EduSubjectService service, String title,String pid){
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("title",title);
        wrapper.eq("parent_id",pid);
        EduSubject eduSubject =   service.getOne(wrapper);
        return  eduSubject;
    }
    //    读取表头
    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        System.out.println("表头+"+headMap);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }
}
